<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

namespace App\HttpController\adminapi\setting;

use App\HttpController\adminapi\AuthController;
use App\Model\Admin\SystemMenusModel;
use App\Services\System\SystemMenusServices;
use EasySwoole\Http\Message\Status;
use EasySwoole\Validate\Validate;

/**
 * Description of SystemMenus
 *
 * @author zike
 */
class SystemMenus extends AuthController {

    //put your code here


    protected function getValidateRule(?string $action): ?Validate {
	switch ($action) {
	    case 'login':
		$validate = new Validate();
//                $validate->addColumn('imgcode')->required();
//                $validate->addColumn('account')->required()->alphaDash();
//                $validate->addColumn('pwd')->required();
		return $validate;
		break;
	}
	return null;
    }

    /**
     * 获取左侧菜单数据
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function menus() {
	[$menus, $unique] = SystemMenusServices::getInstance()->getMenusList($this->adminInfo->roles, $this->adminInfo->level);
	return $this->writeJson(Status::CODE_OK, ['menus' => $menus, 'unique' => $unique], 'ok');
    }

    /**
     * 菜单展示列表
     * @return \think\Response
     */
    public function index() {
	$where = [
	    'is_show' => $this->request()->getRequestParam('is_show') ?? '',
	    'keyword' => $this->request()->getRequestParam('keyword') ?? ''
	];
	$ret = SystemMenusServices::getInstance()->getList($where);
	return $this->writeJson(Status::CODE_OK, $ret, 'ok');
    }

    /**
     * 显示创建资源表单页.
     *
     * @return mix
     */
    public function create() {

	$ret = SystemMenusServices::getInstance()->createMenus();
	return $this->writeJson(Status::CODE_OK, $ret, 'ok');
    }

    /**
     * 修改菜单权限表单获取
     * @param int $id
     * @return \think\Response
     */
    public function edit($id)
    {
	$id = intval($this->request()->getRequestParam('id'));
	if (!$id) {
	    return $this->writeJson(Status::CODE_BAD_REQUEST, [], '缺少修改参数');
	}
	$ret = SystemMenusServices::getInstance()->updateMenus($id);
	return $this->writeJson(Status::CODE_OK, $ret, 'ok');
    }

    /**
     * 保存菜单权限
     * @return mixed
     */
    public function save() {
	$data = [
	    'menu_name' => $this->request()->getRequestParam('menu_name') ?? '',
	    'controller' => $this->request()->getRequestParam('controller') ?? '',
	    'module' => $this->request()->getRequestParam('module') ?? 'admin',
	    'action' => $this->request()->getRequestParam('action') ?? '',
	    'icon' => $this->request()->getRequestParam('icon') ?? '',
	    'params' => $this->request()->getRequestParam('params') ?? '',
	    'path' => $this->request()->getRequestParam('path') ?? [],
	    'menu_path' => $this->request()->getRequestParam('menu_path') ?? '',
	    'api_url' => $this->request()->getRequestParam('api_url') ?? '',
	    'methods' => $this->request()->getRequestParam('methods') ?? '',
	    'unique_auth' => $this->request()->getRequestParam('unique_auth') ?? '',
	    'header' => $this->request()->getRequestParam('header') ?? '',
	    'is_header' => $this->request()->getRequestParam('is_header') ?? 0,
	    'pid' => $this->request()->getRequestParam('pid') ?? 0,
	    'sort' => $this->request()->getRequestParam('sort') ?? 0,
	    'auth_type' => $this->request()->getRequestParam('auth_type') ?? 0,
	    'access' => $this->request()->getRequestParam('access') ?? 1,
	    'is_show' => $this->request()->getRequestParam('is_show') ?? 0,
	    'is_show_path' => $this->request()->getRequestParam('is_show_path') ?? 0,
	];

	if (!$data['menu_name'])
	    return $this->writeJson(Status::CODE_BAD_REQUEST, [], '请填写按钮名称');
	$data['path'] = implode('/', $data['path']);
	$model = SystemMenusModel::create();
	if ($model->data($data, false)->save()) {
	    return $this->writeJson(Status::CODE_OK, [], '添加成功');
	} else {
	    return $this->writeJson(Status::CODE_BAD_REQUEST, [], '添加失败');
	}
    }

    /**
     * 修改菜单
     * @param $id
     * @return mixed
     */
    public function update()
    {
	$id = intval($this->request()->getRequestParam('id'));
        if (!$id || !($menu = SystemMenusModel::create()->get($id)))
	    return $this->writeJson(Status::CODE_BAD_REQUEST, [], '数据不存在');
	$data = [
	    'menu_name' => $this->request()->getRequestParam('menu_name') ?? '',
	    'controller' => $this->request()->getRequestParam('controller') ?? '',
	    'module' => $this->request()->getRequestParam('module') ?? 'admin',
	    'action' => $this->request()->getRequestParam('action') ?? '',
	    'icon' => $this->request()->getRequestParam('icon') ?? '',
	    'params' => $this->request()->getRequestParam('params') ?? '',
	    'path' => $this->request()->getRequestParam('path') ?? [],
	    'menu_path' => $this->request()->getRequestParam('menu_path') ?? '',
	    'api_url' => $this->request()->getRequestParam('api_url') ?? '',
	    'methods' => $this->request()->getRequestParam('methods') ?? '',
	    'unique_auth' => $this->request()->getRequestParam('unique_auth') ?? '',
	    'header' => $this->request()->getRequestParam('header') ?? '',
	    'is_header' => $this->request()->getRequestParam('is_header') ?? 0,
	    'pid' => $this->request()->getRequestParam('pid') ?? 0,
	    'sort' => $this->request()->getRequestParam('sort') ?? 0,
	    'auth_type' => $this->request()->getRequestParam('auth_type') ?? 0,
	    'access' => $this->request()->getRequestParam('access') ?? 1,
	    'is_show' => $this->request()->getRequestParam('is_show') ?? 0,
	    'is_show_path' => $this->request()->getRequestParam('is_show_path') ?? 0,
	];
	if (!$data['menu_name'])
	    return $this->writeJson(Status::CODE_BAD_REQUEST, [], '请填写按钮名称');
        $data['path'] = implode('/', $data['path']);
        if ($menu->update($data))
	    return $this->writeJson(Status::CODE_OK, [], '修改成功');
        else
	    return $this->writeJson(Status::CODE_BAD_REQUEST, [], '修改失败');
    }

    /**
     * 获取一条菜单权限信息
     * @param int $id
     * @return \think\Response
     */
    public function read() {
	$id = intval($this->request()->getRequestParam('id'));
	if (!$id) {
	    return $this->writeJson(Status::CODE_BAD_REQUEST, [], '数据不存在');
	}
	$ret = SystemMenusModel::create()->get($id);
	return $this->writeJson(Status::CODE_OK, $ret ?? [], 'ok');
    }

    /**
     * 删除指定资源
     *
     * @param int $id
     * @return \think\Response
     */
    public function delete() {
	$id = intval($this->request()->getRequestParam('id'));
	if (!$id) {
	    return $this->writeJson(Status::CODE_BAD_REQUEST, [], '参数错误，请重新打开');
	}

	if (!SystemMenusModel::create()->destroy($id)) {
	    return $this->writeJson(Status::CODE_BAD_REQUEST, [], '删除失败,请稍候再试!');
	} else {
	    return $this->writeJson(Status::CODE_OK, [], '删除成功');
	}
    }
    
    /**
     * 显示和隐藏
     * @param $id
     * @return mixed
     */
    public function show()
    {
	$id = intval($this->request()->getRequestParam('id'));
        if (!$id || !($menu = SystemMenusModel::create()->get($id)))
	    return $this->writeJson(Status::CODE_BAD_REQUEST, [], '数据不存在');
	
        $show = intval($this->request()->getRequestParam('is_show'));
        if ($menu->update(['is_show' => $show]))
	    return $this->writeJson(Status::CODE_OK, [], '修改成功');
        else
	    return $this->writeJson(Status::CODE_BAD_REQUEST, [], '修改失败');
    }

}
